package C052;

import java.io.*;
import java.util.Scanner;

/**
 * ClassName: C01
 * Package: C052
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/3/18 19:51
 * @Version 1.0
 * @Since 1.0
 */
public class C01 {
    public static int MAXN = 1000001;

    // 存数的数组
    public static int[] arr = new int[MAXN];


    public static int[] stack = new int[MAXN];

    public static int[][] ans = new int[MAXN][2];

    // n表示arr的有效的范围，r是控制栈的变量
    public static int n, r;

    public static void main(String[] args) throws IOException {
//        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        StreamTokenizer in = new StreamTokenizer(br);
//        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
//        while (in.nextToken() != StreamTokenizer.TT_EOF) {
//            n = (int) in.nval;
//            for (int i = 0; i < n; i++) {
//                in.nextToken();
//                arr[i] = (int) in.nval;
//            }
//            compute();
//            for (int i = 0; i < n; i++) {
//                out.println(ans[i][0] + " " + ans[i][1]);
//            }
//        }
//        out.flush();
//        out.close();
//        br.close();

        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        compute2();
        for (int i = 0; i < n; i++) {
            System.out.println(ans[i][0] + " " + ans[i][1]);
        }
    }


    private static void compute() {
        r = 0;
        stack[r] = 0;
        for (int i = 1; i < n; i++) {
            while (r >= 0 && arr[stack[r]] >= arr[i]) {
                // 否则弹出，直接计算答案
                ans[stack[r]][1] = i;
                ans[stack[r]][0] = --r == -1 ? -1 : stack[r];
            }
            stack[++r] = i;
        }

        // 栈需要清空
        while (r != -1) {
            ans[stack[r]][1] = -1;
            ans[stack[r]][0] = --r >= 0 ? stack[r] : -1;
        }

        // 调整阶段
        // 左边的不用判断，因为一定是正确的(根据之前的逻辑)
        for (int i = 0; i < n; i++) {
            while (ans[i][1] != -1 && arr[ans[i][1]] == arr[i]) {
                ans[i][1] = ans[ans[i][1]][1];
            }
        }
    }

    // 与1相反，求出最近的大于的位置
    private static void compute2() {
        r = 0;
        stack[r] = 0;
        for (int i = 1; i < n; i++) {
            while (r >= 0 && arr[stack[r]] <= arr[i]) {
                // 否则弹出，直接计算答案
                ans[stack[r]][1] = i;
                ans[stack[r]][0] = --r == -1 ? -1 : stack[r];
            }
            stack[++r] = i;
        }

        // 栈需要清空
        while (r != -1) {
            ans[stack[r]][1] = -1;
            ans[stack[r]][0] = --r >= 0 ? stack[r] : -1;
        }

        // 调整阶段
        // 左边的不用判断，因为一定是正确的(根据之前的逻辑)
        for (int i = 0; i < n; i++) {
            while (ans[i][1] != -1 && arr[ans[i][1]] == arr[i]) {
                ans[i][1] = ans[ans[i][1]][1];
            }
        }
    }
}
